<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>抽奖管理</title>

    <style>
        html {
            background: url(../images/<?php echo $type;?>.jpg) no-repeat center center fixed;
            -webkit-background-size: cover;
            -moz-background-size: cover;
            -o-background-size: cover;
            background-size: cover;
        }

        .lottery-user {
            position: fixed;
            bottom: 40%;
            font-size: 120px;
            left:36.5%;
            width: 100%;
            color: #fff;
        }
    </style>
</head>
<body>

    <div class="lottery-user">

    </div>
    <audio id="myaudio" src="../images/lotoery.wav" controls="controls" loop="false" hidden="true" ></audio>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="../bootstrap/jquery.min.js"></script>

        <script>
            var t;
            var lotteryUser = '';
            var running = false;
            var myAuto = document.getElementById('myaudio');
            var users = <?php echo json_encode($users);?>;
            function timedCount() {
                var max = users.length;
                var random = parseInt(Math.random()*(max+1),10);
                $(".lottery-user").html(users[random]);
                t = setTimeout("timedCount()", 10)
            }

            function stopCount() {
                clearTimeout(t)
            }
            function lottery() {
                var type = <?php echo $type;?>;
                running = true;
                $.ajax({
                    url:'/user.php?action=lottery',
                    type:'POST', //GET
                    async:true,    //或false,是否异步
                    data:{
                        type:type
                    },
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                    beforeSend: function(){
                        timedCount();
                    },
                    success:function(data){
                        lotteryUser   = data.message;
                    }
                });
            }

            $("body").keydown(function (e) {
                if(!e) var e = window.event;
                if(e.keyCode==32 || e.keyCode==13 ){
                    if (running == false) {
                        myAuto.play();
                        getUsers();
                    } else {
                        stopCount();
                        $(".lottery-user").html(lotteryUser);
                        myAuto.pause();
                        running = false;
                    }
                }
            });

            function getUsers() {
                $.ajax({
                    url:'/user.php?action=lists',
                    type:'GET', //GET
                    async:true,    //或false,是否异步
                    timeout:5000,    //超时时间
                    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text

                    success:function(data){
                        users = data.message;
                        lottery();
                    }
                });
            }
        </script>
</body>
</html>